﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.DBOper
{
    public class Cls_Section
    {
        private int sectionId;

        public int SectionId
        {
            get { return sectionId; }
            set { sectionId = value; }
        }

        private string sectionName;

        public string SectionName
        {
            get { return sectionName; }
            set { sectionName = value; }
        }

    }


    public class OperateSection
    {

        /// <summary>
        /// 查询所有节点信息
        /// </summary>
        public List<Cls_Section> GetSectionList()
        {
            List<Cls_Section> testlist = new List<Cls_Section>();
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {

                try
                {
                    mydb.Open();
                    string sql = "select * from T_Section where SectionId>0";

                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        Cls_Section rd = new Cls_Section();

                        rd.SectionId = Convert.ToInt32(mydb.DataReader["SectionId"]);

                        rd.SectionName = mydb.DataReader["SectionName"].ToString();
            
                        testlist.Add(rd);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    testlist = null;
                    Mdl_Class.WriteLog.dbErrLog("List<Cls_Section> GetSectionList()", ex.ToString());
                }
                return testlist;
            }
        }


        /// <summary>
        /// 查询所有节点信息
        /// </summary>
        public Cls_Section GetSectionListBySectionName(string sectionName)
        {
            Cls_Section testsection=null;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {

                try
                {
                    mydb.Open();
                    string sql = string.Format("select * from T_Section where SectionName='{0}'", sectionName);

                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        testsection = new Cls_Section();

                        testsection.SectionId = Convert.ToInt32(mydb.DataReader["SectionId"]);

                        testsection.SectionName = mydb.DataReader["SectionName"].ToString();

                      
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    testsection = null;
                    Mdl_Class.WriteLog.dbErrLog("List<Cls_Section> GetSectionList()", ex.ToString());
                }
                return testsection;
            }
        }


        /// <summary>
        /// 添加节点信息
        /// </summary>
        public int AddSection(string sectionname)
        {
            int i = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("insert into T_Section(SectionName,IsUse) values('{0}','{1}')", sectionname, 1);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" AddSection(string sectionname)", ex.ToString());
                }
            }
            return i;
        }



        /// <summary>
        /// 删除测试配置信息
        /// </summary>
        public int DeleteSection(string SectionName)
        {
            int i = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("Update T_Section set IsUse='No' where SectionName='{0}'", SectionName);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DeleteSection(string SectionName)", ex.ToString());
                }
            }
            return i;
        }


        /// <summary>
        /// 删除测试配置信息
        /// </summary>
        public int DeleteSectionBySectionName(string SectionName)
        {
            int i = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("delete from T_Section where SectionName='{0}'", SectionName);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DeleteSectionBySectionName(string SectionName)", ex.ToString());
                }
            }
            return i;
        }

        /// <summary>
        /// 更新测试名称
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public int UpdateSection_Name(string oldName, string newName)
        {
            int i = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("update T_Section set SectionName='{0}' where SectionName='{1}'", newName, oldName);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("UpdateSection_Name(string oldName,string newName)", ex.ToString());
                }
            }
            return i;
        }
    }

}
